package com.hairline.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hairline.bean.*;
import com.hairline.bean.vo.*;
import com.hairline.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxGoodsServiceImpl implements WxGoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;

    @Autowired
    GrouponRulesMapper grouponRulesMapper;

    @Autowired
    IssueMapper issueMapper;

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    GoodsProductMapper goodsProductMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public WxGoodsDetailVO goodsDetail(Integer id, User user) {
        WxGoodsDetailVO wxGoodsDetailVO = new WxGoodsDetailVO();
        //1.查询规格
        List<Specification> specificationList = goodsSpecificationMapper.selectSpecificationsById(id);
        //查询规格详情
        for (Specification s : specificationList) {
            GoodsSpecificationExample example = new GoodsSpecificationExample();
            example.createCriteria().andSpecificationEqualTo(s.getName()).andDeletedEqualTo(false);
            s.setValueList(goodsSpecificationMapper.selectByExample(example));
        }
        wxGoodsDetailVO.setSpecificationList(specificationList);

        //2.查询团购信息
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        grouponRulesExample.createCriteria().andGoodsIdEqualTo(id).andDeletedEqualTo(false);
        List<GrouponRules> list = grouponRulesMapper.selectByExample(grouponRulesExample);
        wxGoodsDetailVO.setGroupon(list);

        //3.查询提问信息
        List<Issue> issues = issueMapper.selectByExample(new IssueExample());
        wxGoodsDetailVO.setIssue(issues);

        //4.查询收藏与否
        boolean ifCollected = false;
        Integer i = collectMapper.selectIfCollectByGoodsIdAndUserId(id, user.getId());
        if (i != 0) {
            ifCollected = true;
        }
        wxGoodsDetailVO.setUserHasCollect(ifCollected);

        //5.查询分享图片
        String share = goodsMapper.selectPicUrlById(id);
        wxGoodsDetailVO.setShareImage(share);

        //6.查询评论
        WxCommentDataVO wxCommentDataVO = new WxCommentDataVO();
        List<WxCommentVO> data = commentMapper.selectWxCommentVOById(id);
        wxCommentDataVO.setData(data);
        long total = new PageInfo<>(data).getTotal();
        wxCommentDataVO.setCount(total);
        wxGoodsDetailVO.setComment(wxCommentDataVO);

        //7.查询商品材质
        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
        goodsAttributeExample.createCriteria().andGoodsIdEqualTo(id);
        List<GoodsAttribute> goodsAttributes = goodsAttributeMapper.selectByExample(goodsAttributeExample);
        wxGoodsDetailVO.setAttribute(goodsAttributes);

        //查询品牌信息
        Brand brand = brandMapper.selectByPrimaryKey(goodsMapper.selectBrandIdById(id));
        wxGoodsDetailVO.setBrand(brand);

        //查询产品信息
        GoodsProductExample goodsProductExample = new GoodsProductExample();
        goodsAttributeExample.createCriteria().andGoodsIdEqualTo(id);
        List<GoodsProduct> goodsProducts = goodsProductMapper.selectByExample(goodsProductExample);
        wxGoodsDetailVO.setProductList(goodsProducts);

        //查询商品详情
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        wxGoodsDetailVO.setInfo(goods);

        return wxGoodsDetailVO;
    }

    @Override
    public WxGoodsRelatedVO goodsRelated(Integer id) {
        WxGoodsRelatedVO wxGoodsRelatedVO = new WxGoodsRelatedVO();
        Integer categoryId = goodsMapper.selectCategoryIdById(id);
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andCategoryIdEqualTo(categoryId);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        wxGoodsRelatedVO.setGoodsList(goods);
        return wxGoodsRelatedVO;
    }

    @Override
    public int selectSortIdById(Integer categoryId) {
        int sortOrder = categoryMapper.selectsortIdById(categoryId);
        return sortOrder;
    }

    @Override
    public List<Goods> selectGoodsByCategoryId(Integer categoryId) {
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andCategoryIdEqualTo(categoryId);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        return goods;
    }

    @Override
    public List<Category> selectGoodsBySortId(int sortOrder, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andSortOrderEqualTo((byte) sortOrder).andLevelEqualTo("L2");
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        return categoryList;
    }
}
